﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace OA.aspx.LanEmail
{
    public partial class LanEmailAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FTD.Unit.PublicMethod.CheckSession();
                //设置上传的附件为空
                FTD.Unit.PublicMethod.SetSessionValue("WenJianList", "");

                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["UserName"]))
                    {
                        this.TextBox2.Text = Request.QueryString["UserName"].ToString();
                    }

                }
                catch
                { }

                //检测是回复或者转发
                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["Type"]))
                    {
                        if (Request.QueryString["Type"].ToString().Trim() == "HuiFu")
                        {
                            FTD.BLL.ERPLanEmail MyModel = new FTD.BLL.ERPLanEmail();
                            MyModel.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                            //设置页面数据
                            this.TextBox1.Text = "Re：" + MyModel.EmailTitle;
                            this.TextBox2.Text = MyModel.FromUser;
                        }
                    }
                }
                catch
                { }
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["Type"]))
                    {
                        if (Request.QueryString["Type"].ToString().Trim() == "ZhuanFa")
                        {
                            FTD.BLL.ERPLanEmail MyModel = new FTD.BLL.ERPLanEmail();
                            MyModel.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                            //设置页面数据
                            this.TextBox1.Text = "RW：" + MyModel.EmailTitle;
                            this.TxtContent.Text = MyModel.EmailContent;
                            FTD.Unit.PublicMethod.SetSessionValue("WenJianList", MyModel.FuJian);
                            FTD.Unit.PublicMethod.BindDDL(this.CheckBoxList1, FTD.Unit.PublicMethod.GetSessionValue("WenJianList"));
                        }
                    }
                }
                catch
                { }
            }
        }
        protected void iButton1_Click(object sender, EventArgs e)
        {
            FTD.BLL.ERPLanEmail MyModel = new FTD.BLL.ERPLanEmail();

            MyModel.EmailTitle = this.TextBox1.Text;
            MyModel.EmailContent = this.TxtContent.Text;
            MyModel.FuJian = FTD.Unit.PublicMethod.GetSessionValue("WenJianList");
            MyModel.FromUser = FTD.Unit.PublicMethod.GetSessionValue("UserName");
            MyModel.EmailState = "未读";
            string[] ToWhoList = this.TextBox2.Text.Trim().Split(',');
            for (int i = 0; i < ToWhoList.Length; i++)
            {
                if (ToWhoList[i].Trim().Length > 0)
                {
                    MyModel.ToUser = ToWhoList[i].Trim();
                    MyModel.Add();
                }
            }

            //写系统日志
            FTD.BLL.ERPRiZhi MyRiZhi = new FTD.BLL.ERPRiZhi();
            MyRiZhi.UserName = FTD.Unit.PublicMethod.GetSessionValue("UserName");
            MyRiZhi.DoSomething = "用户添加新邮件(" + this.TextBox1.Text + ")";
            MyRiZhi.IpStr = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
            MyRiZhi.Add();

            FTD.Unit.MessageBox.ShowAndRedirect(this, "内部邮件添加成功！", "LanEmailShou.aspx");
        }

        protected void iButton2_Click(object sender, EventArgs e)
        {
            string FileNameStr = FTD.Unit.PublicMethod.UploadFileIntoDir(this.FileUpload1, DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName));
            if (FTD.Unit.PublicMethod.GetSessionValue("WenJianList").Trim() == "")
            {
                FTD.Unit.PublicMethod.SetSessionValue("WenJianList", FileNameStr);
            }
            else
            {
                FTD.Unit.PublicMethod.SetSessionValue("WenJianList", FTD.Unit.PublicMethod.GetSessionValue("WenJianList") + "|" + FileNameStr);
            }
            FTD.Unit.PublicMethod.BindDDL(this.CheckBoxList1, FTD.Unit.PublicMethod.GetSessionValue("WenJianList"));
        }
        protected void iButton3_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
                {
                    if (this.CheckBoxList1.Items[i].Selected == true)
                    {
                        FTD.Unit.PublicMethod.SetSessionValue("WenJianList", FTD.Unit.PublicMethod.GetSessionValue("WenJianList").Replace(this.CheckBoxList1.Items[i].Value, "").Replace("||", "|"));
                    }
                }
                FTD.Unit.PublicMethod.BindDDL(this.CheckBoxList1, FTD.Unit.PublicMethod.GetSessionValue("WenJianList"));
            }
            catch
            { }
        }
        protected void iButton4_Click(object sender, EventArgs e)
        {
            //草稿
            FTD.BLL.ERPLanEmail MyModel = new FTD.BLL.ERPLanEmail();

            MyModel.EmailTitle = this.TextBox1.Text;
            MyModel.EmailContent = this.TxtContent.Text;
            MyModel.FuJian = FTD.Unit.PublicMethod.GetSessionValue("WenJianList");
            MyModel.FromUser = FTD.Unit.PublicMethod.GetSessionValue("UserName");
            MyModel.EmailState = "草稿";
            string[] ToWhoList = this.TextBox2.Text.Trim().Split(',');
            for (int i = 0; i < ToWhoList.Length; i++)
            {
                if (ToWhoList[i].Trim().Length > 0)
                {
                    MyModel.ToUser = ToWhoList[i].Trim();
                    MyModel.Add();
                }
            }

            //写系统日志
            FTD.BLL.ERPRiZhi MyRiZhi = new FTD.BLL.ERPRiZhi();
            MyRiZhi.UserName = FTD.Unit.PublicMethod.GetSessionValue("UserName");
            MyRiZhi.DoSomething = "用户添加新邮件(" + this.TextBox1.Text + ")";
            MyRiZhi.IpStr = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
            MyRiZhi.Add();

            FTD.Unit.MessageBox.ShowAndRedirect(this, "内部邮件添加成功！", "LanEmailShou.aspx");
        }
    }
}